﻿using Eleven_Threety_RK.Model;
using MD5Hash;

namespace Eleven_Threety_RK.DAL
{
    public class UserDal
    {
        private readonly Mycontext context;

        public UserDal(Mycontext _context)
        {
            context = _context;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="G"></param>
        /// <returns></returns>
        public int UserAdd(UserInfo G)
        {
            try
            {
                var H = context.Users.Count(I => I.UserName == G.UserName);
                if (H == 0)
                {
                    G.Password = G.Password.GetMD5();
                    context.Users.Add(G);
                    return context.SaveChanges();
                }
                else
                {
                    return -1;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="Password"></param>
        /// <returns></returns>
        public dynamic UserLog(string UserName, string Password)
        {
            try
            {
                var B = context.Users.Count(N => N.UserName == UserName);
                if (B == 0)
                {
                    return -2;
                }
                Password = Password.GetMD5();
                var C=context.Users.Count(S=>S.Password==Password);
                if (C == 0)
                {
                    return -1;
                }
                return context.Users.FirstOrDefault(R => R.UserName == UserName && R.Password == Password);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
